package org.neuedu.dao;

import org.neuedu.bean.Article;
import org.neuedu.bean.Collect;
import org.neuedu.bean.Reply;
import org.neuedu.bean.User;
import org.neuedu.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CollectDaoImpl implements CollectDao {
    @Override
    public int serchCollectArticleByAid(Integer aid) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int count = 0;
        try {
            conn = DBUtils.getInstance().getConnection();
            String sql = "select count(*) count from collect where aid = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,aid);
            rs = ps.executeQuery();
            if(rs.next()){
                count = rs.getInt("count");
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtils.getInstance().close(rs);
            DBUtils.getInstance().close(ps);
            DBUtils.getInstance().close(conn);
        }
        return count;
    }

    @Override
    public int updateCollectArticle(Integer aid, Integer uid) {
        Connection conn = null;
        PreparedStatement ps = null;
        int count = 0;

        try{
            conn = DBUtils.getInstance().getConnection();
            conn.setAutoCommit(false);
            String sql = "insert into collect(aid,uid) values(?,?)";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,aid);
            ps.setInt(2,uid);
            count = ps.executeUpdate();
            conn.commit();
        }catch (Exception e){
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            DBUtils.getInstance().close(ps);
            DBUtils.getInstance().close(conn);
        }
        return count;
    }

    @Override
    public int serchCollectArticleByUid(Integer id) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int count = 0;
        try {
            conn = DBUtils.getInstance().getConnection();
            String sql = "select count(*) count from collect where uid = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            rs = ps.executeQuery();
            if(rs.next()){
                count = rs.getInt("count");
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtils.getInstance().close(rs);
            DBUtils.getInstance().close(ps);
            DBUtils.getInstance().close(conn);
        }
        return count;
    }

    @Override
    public List<Collect> serchCollectArticle(Integer id) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Collect> list = new ArrayList<>();
        try {
            conn = DBUtils.getInstance().getConnection();
            String sql = "select c.aid aid,collecttime,title from collect c join article a " +
                    "on c.aid = a.id " +
                    "where c.uid = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            rs = ps.executeQuery();
            while (rs.next()) {
                Article article = new Article();
                article.setTitle(rs.getString("title"));
                Collect collect = new Collect();
                collect.setArticle(article);
                collect.setAid(rs.getInt("aid"));
                collect.setCollecttime(rs.getString("collecttime"));
                list.add(collect);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtils.getInstance().close(rs);
            DBUtils.getInstance().close(ps);
            DBUtils.getInstance().close(conn);
        }
        return list;
    }
}
